package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.achievement.service.SummaryService;
import com.lanou3g.ptp.api.resolver.annotation.SsoStudent;
import com.lanou3g.ptp.base.service.BaseService;
import com.lanou3g.ptp.entity.*;
import com.lanou3g.ptp.organize.service.ClazzService;
import com.lanou3g.ptp.organize.service.CollegeService;
import com.lanou3g.ptp.organize.service.MajorService;
import com.lanou3g.ptp.organize.service.SchoolService;
import com.lanou3g.ptp.signInDate.service.SignInDateService;
import com.lanou3g.ptp.sso.service.SsoService;
import com.lanou3g.ptp.student.service.StudentService;
import com.lanou3g.ptp.task.service.TaskService;
import com.lanou3g.ptp.teachlink.service.TeachlinkService;
import com.lanou3g.ptp.user.service.UserService;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import com.lanou3g.ptp.vo.AlertBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import com.lanou3g.utils.Word;

@Api(description = "总结接口")
@RestController
public class SummaryController {

    @Reference
    private SummaryService summaryService;

    @Reference
    private TaskService taskService;

    @Reference
    private UserService userService;

    @Reference
    private SsoService ssoService;

    @Reference
    private SchoolService schoolService;
    @Reference
    private StudentService studentService;
    @Reference
    private ClazzService clazzService;
    @Reference
    private MajorService majorService;
    @Reference
    private BaseService baseService;
    @Reference
    private CollegeService collegeService;
    @Reference
    private SignInDateService signInDateService;
    @Reference
    private TeachlinkService teachlinkService;


    @ApiOperation("通过ID查询总结信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "总结id")
    })
    @GetMapping("/summary/{id}")
    public PtpResult findSummaryById(@PathVariable Long id) {

        Summary summary = summaryService.findSummaryById(id);
        Task task = taskService.findById(summary.getTaskId());
        User user = userService.userById(summary.getStudentId());

        Map<String, Object> result = new HashMap<>();
        result.put("id", summary.getId());
        result.put("task_id", task.getName());
        result.put("student_id", user.getName());
        result.put("content", summary.getContent());
        result.put("status", summary.getStatus());

        return PtpResultUtils.ok(result);

    }

    @ApiOperation("查询所有总结信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "显示条数"),
            @ApiImplicitParam(name = "keyword", value = "查询条件")})
    @GetMapping("/summarys")
    public PtpResult findAllSummary(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                    @RequestParam(value = "size", defaultValue = "30") Integer size,
                                    String keyword) {

        PageInfo<Summary> p;
        try {
            if (keyword != null && !keyword.trim().equals("")) {
                p = summaryService.findByKeyword(page, size, keyword);

            } else {
                p = summaryService.findAllSummary(page, size);
            }
//            List<Summary> list = p.getList();
//
//            Map<String,Object> result = new HashMap<>();
//
//            for (Summary summary : list) {
//                Task task = taskService.findById(summary.getTaskId());
//                User user = userService.userById(summary.getStudentId());
//                result.put("id", summary.getId());
//                result.put("task_id",task.getName());
//                result.put("student_id",user.getName());
//                result.put("content", summary.getContent());
//                result.put("status", summary.getStatus());
//            }

            return PtpResultUtils.ok(p);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("学生通过id修改总结")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "总结id"),
            @ApiImplicitParam(name = "token", value = "用户信息key值")
    })
    @PutMapping("/summary/{id}")
    public PtpResult updateSummary(@PathVariable Long id,
                                   @RequestBody Summary summary,
                                   @ApiIgnore @SsoStudent Student student) {
        summary.setStudentId(student.getId());
        try {
            summaryService.updateSummary(id, summary);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

//    @ApiOperation("学生添加总结")
//    @PostMapping("/mySummary")
//    public PtpResult insert(Summary summary,Long taskId,String token) {
//        Student student = (Student) ssoService.tokenUser(token);
//        summary.setStudentId(student.getId());
//        summary.setTaskId(taskId);
//        try {
//            summaryService.insert(summary);
//            re turn PtpResultUtils.ok(200);
//        } cat ch (Exception e) {
//            e.printStackTrace();
//            return PtpResultUtils.error(500);
//        }
//
//    }

    @ApiOperation("学生添加总结")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PostMapping("/mySummary")
    public PtpResult insert(@RequestBody Summary summary,
                            @ApiIgnore @SsoStudent Student student) {
        summary.setStudentId(student.getId());
        System.out.println(summary);
        try {
            boolean success = summaryService.insert(summary);
            System.out.println(success);
            return success ? PtpResultUtils.ok() : PtpResultUtils.error(420, "总结添加失败");
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }


    @ApiOperation("通过id删除总结信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "总结id")
    })
    @DeleteMapping("/summary/{id}")
    public PtpResult deleteSummaryId(@PathVariable Long id) {

        try {
            summaryService.deleteSummaryId(id);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }

    @ApiOperation("批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "多个总结id")
    })
    @DeleteMapping("/summaries/{ids}")
    public PtpResult deleteSummaryIds(@PathVariable Long[] ids) {

        try {
            summaryService.deleteSummaryIds(ids);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("学生查询所有总结")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值")
    })
    @GetMapping("/mySummarys")
    public PtpResult myFindAll(String token) {
        try {
            Student student = (Student) ssoService.tokenUser(token);
            List<Summary> summaries = summaryService.MyFindAll(student.getId());
            return PtpResultUtils.ok(summaries);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }

    @ApiOperation("学生按任务ID查询总结")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值"),
            @ApiImplicitParam(name = "taskId", value = "任务ID")
    })
    @GetMapping("/task/{taskId}/mySummary")
    public PtpResult mySummary(@ApiIgnore @SsoStudent Student student,
                               @PathVariable Long taskId) {
        try {
            Summary summary = summaryService.findMySummaryByTaskId(student.getId(), taskId);
            return PtpResultUtils.ok(summary);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }

    //导出word
    @GetMapping("/summary1/{id}")
    public PtpResult findSummaryById1(@PathVariable Long id) {

        try {

            HashMap<String, Object> result = new HashMap<>();
            Summary summary = summaryService.findSummaryById(id);
            result.put("summary", summary);

            Task task = taskService.findById(summary.getTaskId());
            result.put("taskName", task.getName());

            Base base = baseService.findById(id);
            result.put("base", base);

            Task task1 = taskService.findById(id);
            result.put("task1", task1);
            User user1 = userService.userById(task1.getTeacherId1());
            result.put("teacherName", user1.getName());
            Teachlink teachlink = teachlinkService.findById(task1.getTeachlinkId());
            result.put("target", teachlink.getTarget());
            // result.put("type",teachlink.getType());


            Student student = studentService.studentById(summary.getStudentId());
            User user = userService.userById(student.getId());
            result.put("studentName", user.getName());
            result.put("studentNo", user.getNo());

            Clazz calzz = clazzService.findById(student.getClazzId());
            result.put("class", calzz.getName());
            Major major = majorService.findById(calzz.getMajorId());
            result.put("major", major.getName());


            Major major1 = majorService.findById(id);
            result.put("major1", major1);
            College college = collegeService.findById(major1.getCollegeId());
            result.put("college", college.getSchoolId());
            School school = schoolService.findById(college.getSchoolId());
            result.put("schoolName", school.getName());

            Task task2 = taskService.findById(id);
            result.put("task2", task2);
            SignInDate signInDate = signInDateService.findById(task2.getId());
            result.put("date", signInDate.getDate());

            summaryService.newWord(result);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }


    @ApiOperation("提醒某个学生上传总结报告")
    @PostMapping("/summary/alert")
    public PtpResult alertDiary(@RequestBody AlertBody alert) {
        // TODO: 2019/8/29 总结上传提醒
        boolean b = this.summaryService.alertSummary(alert);
        return b ? PtpResultUtils.ok() : PtpResultUtils.error(505, "发送失败");
    }

}
